<?php
namespace app\client\controller;
use app\common\lib\Redis;

class Invite extends Base {
    public function __construct() {
        parent::__construct();
    }
    
    public function numbers() {
        $data = [
            'number_1' => 0,
            'amount_1' => 0,
            'number_2' => 0,
            'amount_2' => 0,
            'number_3' => 0,
            'amount_3' => 0,
            
            'amount' => 0,
            'number' => 0
        ];
        
        $logs = model('InviteLog')->findAll(['user_id' => $this->userid], ['type', 'amount']);
        
        foreach ($logs as $log) {
            $data['number_' . $log['type']] += 1;
            $data['amount_' . $log['type']] += $log['amount'];
            
            $data['number'] += 1;
            $data['amount'] += $log['amount'];
        }
        
        //$data['number'] = model('InviteLog')->where(['user_id' => $this->userid])->count();
        //$data['amount'] = model('UserWallet')->where(['user_id' => $this->userid])->value('invite_amount');
        return successResult($data);
    }
    
    public function log() {
        $params = request()->get();
        $pk = isset($params['lastPk']) ? intval($params['lastPk']) : 0;
        $pageSize = isset($params['pageSize']) ? intval($params['pageSize']) : 20;
        
        $where = [['user_id', '=', $this->userid]];
        
        if($pk > 0) {
            $where[] = ['id', '<', $pk];
        }
        
        $logs = model('InviteLog')->where($where)->order('id desc')->field(['amount', 'hit_user_id', 'add_time', 'type'])->limit($pageSize)->select()->toArray();
        
        if(!empty($logs)) {
            $userIds = [];
            foreach ($logs as $log) {
                if(!in_array($log['hit_user_id'], $userIds)) {
                    array_push($userIds, $log['hit_user_id']);
                }
            }
            
            $userList = model('User')->findAll(['user_id' => $userIds], ['avatar', 'nickname', 'user_id']);
            
            foreach ($logs as $k => $log) {
                foreach ($userList as $user) {
                    if($log['hit_user_id'] == $user['user_id']) {
                        $logs[$k]['user'] = $user;
                    }
                }
            }
        }
        
        
        //$resultList = model('User')->where(['user_id' => $logUserIds])->field(['user_id', 'avatar', 'register_time', 'nickname'])->order('user_id desc')->limit($pageSize)->select()->toArray();
        
        return successResult(['list' => $logs]);
    }
    
    public function qrcode() {
        $scene = '?from=invite&inviter=' . $this->userid;
        
        $redis = new Redis();
        $ACCESS_TOKEN = $redis->get('wx_access_token');
        $redis->close();
        if(empty($ACCESS_TOKEN)) {
            return errorResult();
        }

        $ch = curl_init('https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=' . $ACCESS_TOKEN);
        curl_setopt($ch, CURLOPT_POST, 1); //设置为POST方式
        curl_setopt($ch, CURLOPT_HTTPHEADER, ['Content-Type: application/json']);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode(['scene' => $scene, 'width' => 280, 'is_hyaline' => true]));//POST数据
        $result = curl_exec($ch);
        if(empty($result)) {
            return errorResult();
        }
        if(stripos($result, '{') === 0) {
            return errorResult();
        }
        
        return $result;
    }
    
}